package com.lj.controller;

import com.github.pagehelper.PageInfo;
import com.lj.pojo.Projectlj;
import com.lj.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/project")
public class ProjectAction {
    public static final int PAGE_SIZE=5;
    @Autowired
    ProjectService projectService;
    @RequestMapping("/selectProject")
    public String selectProject(HttpServletRequest request){
        //得到第一页数据
        PageInfo info=projectService.selectProject(1,PAGE_SIZE);
        request.setAttribute("info",info);
        return "admin/projectView";
    }
    //ajax分页翻页处理
    @ResponseBody
    @RequestMapping("/ajaxSplit")
    public void ajaxSplit(int page, HttpSession session){
        //取得当前page参数页面的数据
        PageInfo info=projectService.selectProject(page,PAGE_SIZE);
        session.setAttribute("info",info);
    }
    @RequestMapping(value = "/insert")
    public String insert(Projectlj projectlj){
        int num=-1;
        num=projectService.insertProject(projectlj);
        return "redirect:/project/selectProject.action";
    }
    @RequestMapping("/delete")
    public String delete(String projectName){
        int nums=-1;
        nums=projectService.delete(projectName);
        return"forward:/project/selectProject.action";
    }
    @ResponseBody
    @RequestMapping("/getProjectById")
    public Projectlj getProjectById(String projectName, HttpServletRequest request ){
        Projectlj projectlj=projectService.getById(projectName);
        return projectlj;
    }
    @RequestMapping("/update")
    public String update(Projectlj projectlj){
        int nums= projectService.update(projectlj);
        return "redirect:/project/selectProject.action";
    }
}
